using Npgsql;
using System;
using Gtk;
using System.Data;

namespace PTransaction
{
	

	public partial class TransactionView : Gtk.Window
	{
		private NpgsqlConnection connection;
		private NpgsqlTransaction transaction;
		
		public TransactionView () : base(Gtk.WindowType.Toplevel)
		{
			this.Build ();
			
			connection = DbConnection.Instance.Connection;
			
			goForwardAction.Activated += delegate {
				transaction = connection.BeginTransaction();
				labelStatus.LabelProp = "BeginTransaction";
			};
			
			applyAction.Activated += delegate {
				transaction.Commit();
				labelStatus.LabelProp = "Commit";
			};
			
			executeAction.Activated += delegate {
				string sql = textView.Buffer.Text;
				NpgsqlCommand command = connection.CreateCommand();
				command.CommandText = sql;
				command.CommandTimeout = 20;
				try {
					command.ExecuteNonQuery;
					labelStatus.LabelProp = "Command Ok";
				} catch (Exception e) {
					MessageDialog messageDialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, e.Message);
					messageDialog.Run();
					messageDialog.Destroy();
				}
			};
			
			cancelAction.Activated += delegate {
				transaction.Rollback();
				labelStatus.LabelProp = "Rollback";
			};
		}
	}
}
